package com.xinminda.dcf.ui.main.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import com.xinminda.dcf.R;
import com.xinminda.dcf.app.AppConstant;
import com.xinminda.dcf.bean.ColumnsBean;
import com.xinminda.dcf.ui.base.BaseFragment;
import com.xinminda.dcf.ui.base.BaseFragmentAdapter;
import com.xinminda.dcf.ui.news.contract.NewsMainContract;
import com.xinminda.dcf.ui.news.fragment.NewsFrament;
import com.xinminda.dcf.ui.news.model.NewMainModel;
import com.xinminda.dcf.ui.news.presenter.NewMainPresenter;
import com.xinminda.dcf.utils.MyUtils;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;


public class NewsMainFragment extends BaseFragment<NewMainPresenter, NewMainModel> implements NewsMainContract.View {

    @BindView(R.id.tabs)
    TabLayout tabs;

    @BindView(R.id.add_channel_iv)
    ImageView addChannelIv;

    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private BaseFragmentAdapter fragmentAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_news;
    }

    @Override
    protected void initView() {
        mPresenter.lodeMineChannelsRequest();
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel);
    }


    @Override
    public void returnMineNewsChannels(List<ColumnsBean> columnslist) {

        if(columnslist != null){
            List<String> channelNames = new ArrayList<>();
            List<Fragment> mNewsFragmentList = new ArrayList<>();
            for (int i = 0; i < columnslist.size(); i++) {
                channelNames.add(columnslist.get(i).getName());
                mNewsFragmentList.add(createListFragments(columnslist.get(i)));
            }

            if(fragmentAdapter==null) {
                fragmentAdapter = new BaseFragmentAdapter(getChildFragmentManager(), mNewsFragmentList, channelNames);
            }else{
                //刷新fragment
                fragmentAdapter.setFragments(getChildFragmentManager(),mNewsFragmentList,channelNames);
            }
            viewPager.setAdapter(fragmentAdapter);
            tabs.setupWithViewPager(viewPager);
            MyUtils.dynamicSetTabLayoutMode(tabs);
            setPageChangeListener();
        }

    }


    private void setPageChangeListener() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private NewsFrament createListFragments(ColumnsBean columnsBean) {
        NewsFrament fragment = new NewsFrament();
        Bundle bundle = new Bundle();
        bundle.putInt(AppConstant.NEWS_ID, columnsBean.getId());
        fragment.setArguments(bundle);
        return fragment;
    }

}
